package com.lsl.controller;

import com.lsl.entity.BlogVote;
import com.lsl.entity.Result;
import com.lsl.service.BlogVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vote-service")
public class VoteController {

    @Autowired
    private BlogVoteService blogVoteService;

    @GetMapping("/type/{blogId}/{userId}")
    public Result<Integer> getVoteType(
            @PathVariable Integer blogId,
            @PathVariable Integer userId) {
        return Result.success(blogVoteService.getVoteType(blogId, userId));
    }

    @PostMapping("/vote")
    public Result<Boolean> vote(@RequestBody BlogVote blogVote) {
        boolean success = blogVoteService.vote(
            blogVote.getBlogID(),
            blogVote.getUserID(),
            blogVote.getVoteType()
        );
        return success ? Result.success(true) : Result.error(500, "投票失败");
    }

    @PostMapping("/change")
    public Result<Boolean> changeVote(@RequestBody BlogVote blogVote) {
        boolean success = blogVoteService.changeVote(
            blogVote.getBlogID(),
            blogVote.getUserID(),
            blogVote.getVoteType()
        );
        return success ? Result.success(true) : Result.error(500, "修改投票失败");
    }

    @DeleteMapping("/remove/{blogId}/{userId}")
    public Result<Boolean> removeVote(
            @PathVariable Integer blogId,
            @PathVariable Integer userId) {
        boolean success = blogVoteService.removeVote(blogId, userId);
        return success ? Result.success(true) : Result.error(500, "撤销投票失败");
    }
}
